package priv.tuyou.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期处理工具类
 * @ClassName: DateUtil   
 * @Description: 对日期的各种操作   
 * @author 涂有  
 * @date Jul 8, 2013 11:43:36 AM   
 *
 */
public class DateUtil {
	
	/** 默认的时间日期格式 */
	public static final SimpleDateFormat DEFAULT_DATE_TIME_FOMRAT = new SimpleDateFormat("yyyyMMddhhmmss");
	public static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss_SSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	public static final SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat HH_mm_ss = new SimpleDateFormat("HH:mm:ss");
	public static final SimpleDateFormat yyyy年MM月dd日 = new SimpleDateFormat("yyyy年MM月dd日");
	
	/**
	 * 获取月份的第一天
	 * @return
	 */
	public static String getFirstDayByMonth() {
		Calendar cal = Calendar.getInstance();
		// 当前月的最后一天
		cal.set(Calendar.DATE, 1);
		cal.roll(Calendar.DATE, -1);
		// 当前月的第一天
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTime = cal.getTime();
		String beginTime1 = yyyy_MM_dd.format(beginTime);
		return beginTime1;
	}
	
	/**
	 * 获取月份的最后第一天
	 * @return
	 */
	public static String getLastDayByMonth() {
		Calendar cal = Calendar.getInstance(); 
         //当前月的最后一天   
         cal.set( Calendar.DATE, 1 );
         cal.roll(Calendar.DATE, - 1 );
         Date endTime=cal.getTime();
         String endTime1=yyyy_MM_dd.format(endTime);
          //当前月的第一天          
         cal.set(GregorianCalendar.DAY_OF_MONTH, 1); 
         return endTime1;
	}
	
	/**
	 * parseStringToDate(根据指定的日期字符串返回日期对象)
	 * @param  dateString 要解析的日期字符串
	 * @return 返回这个dateString所代表的日期对象
	 * @throws ParseException 如果转换出现异常, 则返回<code>null</code>对象
	 */
	public static Date parseStringToDate(String dateString) {
		try {
			return yyyy_MM_dd_HH_mm_ss.parse(dateString);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * parseStringToDate(根据指定的日期字符串返回日期对象)
	 * @param  dateString 要解析的日期字符串
	 * @return 返回这个dateString所代表的日期对象
	 * @throws ParseException 如果转换出现异常, 则返回<code>null</code>对象
	 */
	public static Date parseShortStringToDate(String dateString) {
		try {
			return yyyy_MM_dd.parse(dateString);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * parseStringToDate(根据一个指定的日期字符串返回一个指定格式的日期对象)
	 * @param  dateStr 指定的日期格式
	 * @param  pattern 指定的字符串格式
	 * @return 返回指定的日期格式
	 * @throws ParseException 如果转换出现异常, 则返回<code>null</code>对象
	 */
	public static Date parseStringToDate(String dateStr, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			try {
				return sdf.parse(dateStr);
			} catch (ParseException e) {
				return null;
			}
	}
	
	/**
	 * parseSimpleStringToDate(根据一个简单的的日期字符串返回日期对象 例如:yyyy-MM-dd)
	 * @param  simpleDateString 要解析的简单日期字符串
	 * @return 返回这个simpleDateString所代表的日期对象
	 * @throws ParseException 如果转换出现异常, 则返回<code>null</code>对象
	 */
	public static Date parseSimpleStringToDate(String simpleDateString) {
		try {
			if(simpleDateString.indexOf(':') > 0){
				
				return yyyy_MM_dd_HH_mm_ss.parse(simpleDateString);
			}
			return yyyy_MM_dd_HH_mm_ss.parse(new StringBuilder(simpleDateString).append(" 00:00:00").toString());
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * 根据日期计算年龄
	 * @Title: getAge 
	 * @param @param date
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public static int getAge(java.util.Date date) {
		try {
			Calendar cal = Calendar.getInstance();
			
			int yearNow = cal.get(Calendar.YEAR);
	        int monthNow = cal.get(Calendar.MONTH)+1;
	        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
	       
	        cal.setTime(date);
	        int yearBirth = cal.get(Calendar.YEAR);
	        int monthBirth = cal.get(Calendar.MONTH);
	        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

	        int age = yearNow - yearBirth;

	        if (monthNow <= monthBirth) {
	            if (monthNow == monthBirth) {
	                //monthNow==monthBirth
	                if (dayOfMonthNow < dayOfMonthBirth) {
	                    age--;
	                }
	            } else {
	                //monthNow>monthBirth
	                age--;
	            }
	        }
	        return age;
		} catch(Exception e) {
			return 0;
		}
	}

	/**
	 * 获取一天的开始时间以日期返回
	 * @return
	 */
	public static Date getDayStart(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return parseStringToDate(formatDateToShortString(date) + " 00:00:00");
	}
	
	/**
	 * 获取一天的结束时间以日期返回
	 * @return
	 */
	public static Date getDayEnd(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return parseStringToDate(formatDateToShortString(date) + " 23:59:59");
	}
	
	/**
	 * 获取本周的开始时间
	 * @return
	 */
	public static Date getThisWeekStartDate() {
		try {
			Calendar thisRegion = Calendar.getInstance();
			thisRegion.set(Calendar.DAY_OF_WEEK, 2);
			return  DateUtil.getDayStart(thisRegion.getTime());
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 获取本月的开始时间
	 * @return
	 */
	public static Date getThisMonthStartDate() {
		try {
			Calendar thisRegion = Calendar.getInstance();
			thisRegion.set(Calendar.DAY_OF_MONTH, 1);
			return DateUtil.getDayStart(thisRegion.getTime());
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 把日期转为字符串
	 * @param date
	 * @return
	 */
	public static String formatDateToSimpleString(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return yyyy_MM_dd_HH_mm_ss.format(date);
	}
	
	/**
	 * 把日期转为字符串
	 * @param date
	 * @return
	 */
	public static String formatDateToShortString(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return yyyy_MM_dd.format(date);
	}
	
	/**
	 * 把日期转为字符串
	 * @param date
	 * @return
	 */
	public static String formatDateToTimeString(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return HH_mm_ss.format(date);
	}
	
	/**
	 * 把日期转为完整字符串
	 * @param date
	 * @return
	 */
	public static String formatDateToFullString(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return yyyy_MM_dd_HH_mm_ss_SSS.format(date);
	}
	
	/**
	 * 把日期转为字符串
	 * @param date
	 * @return
	 */
	public static String formatDateToShortString_ZH_CN(Date date){
		
		if(date == null){
			
			return null;
		}
		
		return yyyy年MM月dd日.format(date);
	}
	
	public static String dayOfWeek(Date date){
		
		Calendar can = Calendar.getInstance();
		can.setTime(date);
		switch (can.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			return "星期一";
		case 2:
			return "星期二";
		case 3:
			return "星期三";
		case 4:
			return "星期四";
		case 5:
			return "星期五";
		case 6:
			return "星期六";
		default:
			return "星期日";
		}
	}
}
